package com.hwn.datax.controller;

import com.hwn.datax.domain.ServerInfo;
import com.hwn.datax.service.ServerInfoService;
import com.hwn.datax.utils.AlertUtil;
import javafx.concurrent.Task;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.stage.Stage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * @author wnhuang
 * @date 2024/6/2 21:48
 */
@Controller
public class ServerConfigController {

    @FXML
    public RadioButton windowsRadioButton;
    @FXML
    public RadioButton linuxRadioButton;
    @FXML
    public TextField characterSetField;
    @FXML
    public ProgressIndicator loadingIndicator;
    @FXML
    private TextField nameField;

    @FXML
    private TextField serverAddressField;

    @FXML
    private TextField portField;

    @FXML
    private Button decreasePortButton;

    @FXML
    private Button increasePortButton;


    @FXML
    private TextField usernameField;

    @FXML
    private PasswordField passwordField;

    @FXML
    private TextField oggPathField;

    @FXML
    private ComboBox<String> dataSourceComboBox;

    @FXML
    private Button cancelButton;

    @FXML
    private Button testButton;

    @FXML
    private Button confirmButton;

    @Autowired
    ServerInfoService serverInfoService;


    @FXML
    public void initialize() {
        ToggleGroup osGroup = new ToggleGroup();
        windowsRadioButton.setToggleGroup(osGroup);
        linuxRadioButton.setToggleGroup(osGroup);

        // Initialize your components here
        decreasePortButton.setOnAction(event -> adjustPort(-1));
        increasePortButton.setOnAction(event -> adjustPort(1));

        cancelButton.setOnAction(event -> onCancel());
        testButton.setOnAction(event -> onTest());
        confirmButton.setOnAction(event -> onConfirm());

        portField.setText("22");
    }

    private void adjustPort(int adjustment) {
        try {
            int port = Integer.parseInt(portField.getText());
            port += adjustment;
            portField.setText(String.valueOf(port));
        } catch (NumberFormatException e) {
            // Handle invalid port input
            portField.setText("0");
        }
    }

    private void onCancel() {
        // Get the stage and close it
        Stage stage = (Stage) cancelButton.getScene().getWindow();
        stage.close();
    }

    private void onTest() {
        loadingIndicator.setVisible(true);
        // Perform test connection logic
        Task<Boolean> task = new Task<>() {
            @Override
            protected Boolean call() throws Exception {
                serverInfoService.testConnection(getServerInfo());
                return true;
            }
        };

        task.setOnSucceeded(event -> {
            loadingIndicator.setVisible(false);
            AlertUtil.showAlert("提示", "测试连接成功！");
        });

        task.setOnFailed(event -> {
            loadingIndicator.setVisible(false);
            AlertUtil.showAlert("提示", "测试连接失败！" + event.getSource().getException().getMessage(), Alert.AlertType.ERROR);
        });
        // Handle test action
        new Thread(task).start();
    }

    ServerInfo getServerInfo() {
        ServerInfo serverInfo = new ServerInfo();
        serverInfo.setServerIp(serverAddressField.getText());
        serverInfo.setServerOs(windowsRadioButton.isSelected() ? "Windows" : "Linux");
        serverInfo.setServerName(nameField.getText());
        serverInfo.setServerPass(passwordField.getText());
        serverInfo.setServerPort(portField.getText());
        serverInfo.setServerUser(usernameField.getText());
        serverInfo.setCharacterSet(characterSetField.getText());
        return serverInfo;
    }

    private void onConfirm() {
        serverInfoService.save(getServerInfo());
        AlertUtil.showAlert("确认", "保存成功！");
        onCancel();
    }


}
